/*
 * Copyright 2008 Max Kugland
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.splink.library.queue {
	import org.splink.library.distributor.Dtype;
	import org.splink.library.distributor.Devent;
	import org.splink.library.distributor.IDistributor;

	/**
	 * A <code>QEvent</code> is distributed by <code>IQable</code> implementations.
	 * 
	 * @author Max Kugland
	 */
	public class QEvent extends Devent {
		public static const START : Dtype = new Dtype("qStart");		public static const PROGRESS : Dtype = new Dtype("qProgress");		public static const COMPLETE : Dtype = new Dtype("qComplete");		public static const ERROR : Dtype = new Dtype("qError");

		/**
		 * An error message which is empty unless the event's type is <code>ERROR</code>
		 */
		public var errorMessage : String = "";

		/**
		 * Contains the progress information on the <code>IQable</code> item if it 
		 * is the event source. If an <code>IQ</code> is the event source <code>progress</code>
		 * represents the progress of the currently processing item in the <code>IQ</code>
		 */
		public var progress : Progress = new Progress(0, 0);

		/**
		 * Contains the progress information on an <code>IQ</code> which contains <code>IQable</code> items
		 * if the event source is an <code>IQ</code>. If not <code>queueProgress</code> will contain
		 * a default '0' progress.
		 */ 
		public var queueProgress : Progress = new Progress(0, 0);

		private var _id : String;

		/**
		 * @inheritDoc
		 * @param id the id of the event source
		 */
		public function QEvent(source : IDistributor, type : Dtype, id : String) {
			super(source, type);
			_id = id;
		}

		/**
		 * @return the id of the event, which by convention also is the id of the event source 
		 */
		public function get id() : String {
			return _id;
		}

		/**
		 * Clones the <code>QCompleteEvent</code> but allows to set the event source of the cloned event.  
		 * 
		 * @return the cloned <code>QCompleteEvent</code>
		 */
		public function clone(source : IDistributor) : QEvent {
			var e : QEvent = new QEvent(source, type, _id);
			e.progress = progress.clone();
			e.queueProgress = queueProgress.clone();			e.errorMessage = errorMessage;
			return e;
		}
	}
}
